package com.bdqn.service.platformlist.platform;

import bean.Platform;
import bean.PlatformMember;
import com.bdqn.common.utils.ResponseUitl;
import com.bdqn.common.utils.VerifyCodeUtil;
import com.bdqn.service.platformList.AbstractExecutor;
import com.bdqn.service.platformList.push.JiGuangPushUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.*;

//import org.apache.tomcat.util.http.ResponseUtil;
//import org.apache.tomcat.util.http.ResponseUtil;
//import org.apache.tomcat.util.http.ResponseUtil;
//import javax.swing.text.Document;


@Service
public class Haodanjia implements AbstractExecutor {


    JiGuangPushUtil uni_push=new JiGuangPushUtil();
    String accessToken = "";


    Map<String, List<Object>> result_map;
    //买号信息
    List<Object> result_list;


    /**
     * 获取买号
     */
    String listPlatformAccountApi = "/Mobile/BindTb.aspx";

    String loginApi = "api/user/login";



    public Map<String,List<Object>> process(PlatformMember platformMember) {
        int status = login(platformMember);
//        if(status==VerifyCodeUtil.LOGIN_ERROR){
//            //登陆失败(给与提示消息)
//            System.out.println("登录失败！");
//        }
        //登陆成功
        //--获取买号


        if(status == VerifyCodeUtil.LOGIN_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("登录 "+platformMember.getPlatformName()+" 失败/账号密码错误", Collections.emptyList());
                }
            };
        }
        status=getPlatformAccount(platformMember);
        if(status == VerifyCodeUtil.BUYER_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("获取 "+platformMember.getPlatformName()+"平台 获取买号失败/无审核的买号!",Collections.emptyList());
                }
            };
        }
//        if(platformMember.getClientId()!=null && !"".equals(platformMember.getClientId())) {
//            //获取用户ID
//            status = getUserId(platformMember);
//        }
        return result_map;


    }
    public int   login(PlatformMember platformMember){
        String loginApi = "/mobile/login.aspx";
//        String url ="http://154.8.169.159/";
//        platformMember.setUrl();
//        platformMember.setUrl(platformMember.getUrl().substring(0,platformMember.getUrl().length()-18));
        System.out.println("尝试登录中.......");
        uni_push.transmit(platformMember.getClientId(),
                uni_push.productionMessage(platformMember.getNamePinyin(),
                        "正尝试登录"+platformMember.getPlatformName()+"平台...", VerifyCodeUtil.SUCCESS));
        Map<String,String> map =new HashMap<String,String>();
        map.put("mobile",platformMember.getAccount());
        map.put("password",platformMember.getPassword());
//        HashMap result =  ResponseUitl.getByAuthorization(platformMember.getUrl(),null,map,"utf-8");
//        获取Authorization
        List<String> result = ResponseUitl.getCookieByDoPost(platformMember.getUrl(),null ,map , "utf-8");

//        System.out.println("Author"+result);


//
//  accessToken=(String)result.get("data");
        System.out.println("-------cookie-----------");
        System.out.println("Cookie"+result);
        System.out.println("------------");
        System.out.println(result.get(0));
        accessToken=result.get(1);
        System.out.println("------------");
        System.out.println(accessToken);


        if("false".equals(ResponseUitl.pullMessage(result.get(0),"IsSuccess"))){
            uni_push.transmit(platformMember.getClientId(),
                    uni_push.productionMessage(platformMember.getNamePinyin(),
                            "登录"+platformMember.getPlatformName()+"平台失败/账号密码错误", VerifyCodeUtil.LOGIN_ERROR));
            return -1;
        }
        uni_push.transmit(platformMember.getClientId(),
                uni_push.productionMessage(platformMember.getNamePinyin(),
                        "成功登录"+platformMember.getPlatformName()+"平台", VerifyCodeUtil.LOGIN_SUCCESS));
        return 1;
    }



    /**
     * 自动获取买号
     * @return
     */
    public int getPlatformAccount(PlatformMember platformMember){
        Map<String,String> paramsMap =new HashMap<>();
        paramsMap.put("Cookie",accessToken);

        List<String> list= ResponseUitl.getCookieByDoPost("https://m.yijixun.cn/mobile/tasklistLiuLiang.aspx",paramsMap,null,"utf-8");

        result_list=new ArrayList<>();
        String result=list.get(0);

        System.out.println("-------获取买号页面");

        System.out.println(list.get(0));


        if(null == result || "".equals(result)){
            return -2;
        }

        Map<String,Object> value_map=new HashMap<>();
//        if(result!=null && !"".equals(result)){
//            Platform platform=null;
//            Document document = Jsoup.parse(result);
//            Element postList = document.getElementById("mylist");
//            Elements postItems = postList.getElementsByClass("mui-table-view-cell");
//            for (Element postItem : postItems) {
//                platform=new Platform();
//                Elements titleEle = postItem.select(".mui-media-body");
//                String[] text_array=titleEle.text().split(":");
//                String[] name_array=text_array[1].split(" ");
//
//                Elements titleEle1 = postItem.select(".mui-media-body a");
//                String[] href_array=titleEle1.attr("href").split("=");
//                platform.setId(Integer.valueOf(href_array[1]));
//                platform.setName(name_array[1]);
//                value_map.put(href_array[1],name_array[1]);
//                result_list.add(platform);
//            }
//        }

        if(result!=null && !"".equals(result)){
            Platform platform=new Platform();
            Document document = Jsoup.parse(result);
            Element postList = document.getElementById("ddlAccount");
            Elements titleEle = postList.select("option");
            String postItems = titleEle.text();
                platform.setName(postItems);
//                value_map.put(href_array[1],name_array[1]);
                result_list.add(platform);
        }


//        if(platformMember.getClientId()!=null && !"".equals(platformMember.getClientId())){
//            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(platformMember);
//            //缓存 买号信息
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"buyerInfo", JSONObject.toJSONString(value_map));
//            //缓存 登录状态
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"token", accessToken);
//            //缓存 接单次数  最大 20 次 逐次递减
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"orderCount", "10");
//        }
        uni_push.transmit(platformMember.getClientId(),uni_push.productionMessage(platformMember.getNamePinyin(),"获取到"+result_list.size()+"个买号", VerifyCodeUtil.BUYER_SUCCESS));

        result_map = new HashMap<>();
        result_map.put("获取 "+platformMember.getPlatformName()+" 平台买号成功!",result_list);
        return 1;
    }
}
